package com.ssyt.ec.stu.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.Constant;
import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.common.util.SysuserUtil;
import com.ssyt.ec.stu.entity.ClassEntity;
import com.ssyt.ec.stu.entity.UserEntity;
import com.ssyt.ec.stu.service.UserService;
import com.ssyt.ec.sys.entity.RoleEntity;

@WebServlet("/stu/UserServlet")
public class UserServlet extends AppServlet {
	private static final long serialVersionUID = 1L;
	private UserService userService = new UserService();

	public UserServlet() {
		super();
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String command = request.getParameter("command");
		if ("listUser".equals(command)) {
			listUser(request, response);
		} else if ("listRole".equals(command)) {
			listRole(request, response);
		} else if ("listClass".equals(command)) {
			listClass(request, response);
		} else if ("unique".equals(command)) {
			unique(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("getUser".equals(command)) {
			getUser(request, response);
		} else if ("updateUser".equals(command)) {
			updateUser(request, response);
		} else if ("deleteUser".equals(command)) {
			deleteUser(request, response);
		} else if ("open".equals(command)) {
			open(request, response);
		} else if ("resetPassword".equals(command)) {
			resetPassword(request, response);
		} else if ("close".equals(command)) {
			close(request, response);
		}
	}

	/**
	 * 禁用
	 * 
	 * @param request
	 * @param response
	 */
	private void close(HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		int userId = SysuserUtil.getUserId(request);
		int row = userService.close(ids, userId);
		if (row != 0) {
			print(response, row);
		}
	}

	/**
	 * 重置密码
	 * 
	 * @param request
	 * @param response
	 */
	private void resetPassword(HttpServletRequest request,
			HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		int userId = SysuserUtil.getUserId(request);
		int row = userService.resetPassword(ids, userId);
		if (row != 0) {
			print(response, row);
		}
	}

	/**
	 * 用户启用
	 * 
	 * @param request
	 * @param response
	 */
	private void open(HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		int userId = SysuserUtil.getUserId(request);
		int row = userService.open(ids, userId);
		if (row != 0) {
			print(response, row);
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param request
	 * @param response
	 */
	private void deleteUser(HttpServletRequest request,
			HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		int userId = SysuserUtil.getUserId(request);
		int row = userService.deleteUser(ids, userId);
		if (row != 0) {
			print(response, row);
		}
	}

	/**
	 * 用户修改
	 * 
	 * @param request
	 * @param response
	 */
	private void updateUser(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int sex = Integer.parseInt(request.getParameter("gender"));
		int role = Integer.parseInt(request.getParameter("role"));
		int clazz = Integer.parseInt(request.getParameter("clazz"));
		String memo = request.getParameter("memo");

		int userId = SysuserUtil.getUserId(request);
		List<Object> listParam = new ArrayList<>();
		listParam.add(name);
		listParam.add(sex);
		listParam.add(phone);
		listParam.add(email);
		listParam.add(userId);
		listParam.add(role);
		listParam.add(clazz);
		listParam.add(memo);
		listParam.add(id);

		int row = userService.update(listParam);
		if (row != 0) {
			print(response, row);
		}
	}

	/**
	 * 查单条记录
	 * 
	 * @param request
	 * @param response
	 */
	private void getUser(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		UserEntity userEntity = userService.getUser(id);
		print(response, FastjsonUtil.toJson(userEntity));
	}

	/**
	 * 新增用户
	 * 
	 * @param request
	 * @param response
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) {
		String code = request.getParameter("code");
		String password = Constant.INIT_PASSWORD;
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int sex = Integer.parseInt(request.getParameter("gender"));
		int role = Integer.parseInt(request.getParameter("role"));
		int clazz = Integer.parseInt(request.getParameter("clazz"));
		String memo = request.getParameter("memo");
		UserEntity userEntity = new UserEntity();
		userEntity.setCode(code);
		userEntity.setPassword(password);
		userEntity.setName(name);
		userEntity.setPhone(phone);
		userEntity.setEmail(email);
		userEntity.setSex(sex);
		userEntity.setRole__id(role);
		userEntity.setClass__id(clazz);
		userEntity.setMemo(memo);
		int userId = SysuserUtil.getUserId(request);
		int row = userService.add(userEntity, userId);
		if (row != 0) {
			print(response, row);
		}
	}

	/**
	 * 账号唯一验证
	 * 
	 * @param request
	 * @param response
	 */
	private void unique(HttpServletRequest request, HttpServletResponse response) {
		String param = request.getParameter("param");
		String paramName = request.getParameter("paramName");
		UserEntity user = userService.uniqueCode(param, paramName);
		if (user == null) {
			print(response, "true");
		}
	}

	/**
	 * 班级列表
	 * 
	 * @param request
	 * @param response
	 */
	private void listClass(HttpServletRequest request,
			HttpServletResponse response) {
		List<ClassEntity> list = userService.listClass();
		print(response, FastjsonUtil.toJson(list));
	}

	/**
	 * 角色列表
	 * 
	 * @param request
	 * @param response
	 */
	private void listRole(HttpServletRequest request,
			HttpServletResponse response) {
		List<RoleEntity> list = userService.listRole();
		print(response, FastjsonUtil.toJson(list));
	}

	/**
	 * 用户列表
	 * 
	 * @param request
	 * @param response
	 */
	private void listUser(HttpServletRequest request,
			HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		String sort = request.getParameter("sort");
		String order = request.getParameter("order");
		String name = request.getParameter("name");
		String role = request.getParameter("role");
		String clazz = request.getParameter("clazz");
		String state = request.getParameter("state");
		PageModel<UserEntity> list = userService.listUser(name, role, clazz,
				state, page, rows, sort, order);
		print(response, FastjsonUtil.toJsonWithDateFormat(list, "yyyy-MM-dd"));
	}

}
